﻿using System;
using System.Collections.Generic;
using System.Linq;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Caching;
using ZHDJ.Core.Util;
using ZHDJ.Data.DataCaching;
using ZHDJ.Data.Models.BaseInfo;

namespace SysManage.Module.BaseInfo
{
    public partial class SystemParameterEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "SystemParameterAdd";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                SystemParameter current = DB.SystemParameters.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtID.Readonly = true;
                txtID.Text = current.ID;
                txtDescription.Text = current.Description;
                txtParamName.Text = current.ParamName;
                txtParamValue.Text = current.ParamValue;
             
            }
        }

        #endregion

        #region Events

        private void SaveJobTitle()
        {
            
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                //更新
                SystemParameter item = DB.SystemParameters.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }
       
                item.Description = txtDescription.Text;
                item.ParamName = txtParamName.Text;
                item.ParamValue = txtParamValue.Text;
                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.ID;
                
                DB.SaveChanges();
            }
            else
            {
                var  current = DB.SystemParameters.Find(this.txtID.Text.Trim());
                if (current != null)
                {
                    Alert.Show("参数主键已存在，请重新输入！", String.Empty);
                    return;
                }
                var item = new SystemParameter();
                item.ID = txtID.Text.Trim();
                item.OrderNo = 0;
                item.Description = txtDescription.Text;
                item.ParamName = txtParamName.Text;
                item.ParamValue = txtParamValue.Text;
                item.CreateTime = DateTime.Now;
                item.CreateBy = CurrentUser.ID;
                DB.SystemParameters.Add(item);
                DB.SaveChanges();

            }
            SystemParameterCaching.Clean();
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveJobTitle();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
           
        }
        #endregion

    }
}
